//
//  GradeFullView.m
//  fp
//
//  Created by xingshuai on 2020/11/15.
//

#import "GradeFullView.h"

#import "GradModel.h"
#import "GradeCellCollectionViewCell.h"
static NSString *identifier = @"XAGradeCollectCellID";

@interface GradeFullView () <UIGestureRecognizerDelegate,UICollectionViewDelegate, UICollectionViewDataSource>

// 数据展示
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIImageView *topImg;
@property (nonatomic, strong) UIImageView *contImg;

@end

@implementation GradeFullView

-(UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = [[UILabel alloc]init];
        _topLabel.text = @"选择您所在的年级";
        _topLabel.textColor = [UIColor colorWithHexString:@"#4E75FF"];
        _topLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleMedium size:18];
    }
    return _topLabel;
}

-(UIImageView *)topImg {
    if (!_topImg) {
        _topImg = [[UIImageView alloc]init];
        _topImg.userInteractionEnabled = YES;
        _topImg.image = [UIImage imageNamed:@"grade_alert_top"];
        _topImg.backgroundColor = [UIColor clearColor];
    }
    return _topImg;
}

-(UIImageView *)contImg {
    if (!_contImg) {
        _contImg = [[UIImageView alloc]init];
        _contImg.userInteractionEnabled = YES;
        _contImg.image = [UIImage imageNamed:@"grade_alert"];
        _contImg.backgroundColor = [UIColor clearColor];
    }
    return _contImg;
}

- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collection registerClass:[GradeCellCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.backgroundColor = [UIColor clearColor];
        _collection.showsVerticalScrollIndicator = NO;
        
    }
    return _collection;
}

#pragma mark - UICollectionViewDelegateFlowLayout
// 返回cell的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.collection.frame.size.width-25)/3, (self.collection.frame.size.height-30)/4);
}


#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrGradeList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GradeCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.model = [self.arrGradeList objectAtIndex:indexPath.row];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    __block NSString *str = @"";
    [self.arrGradeList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GradModel *model = obj;
        if (idx == indexPath.row) {
            model.isSelect = YES;
            str = model.gradeString;
        } else {
            model.isSelect = NO;
        }
    }];
    
    if (self.didGradeCellBlock) {
        self.didGradeCellBlock(str);
    }
    
    [self.collection reloadData];
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor colorWithHexString:@"#000000"] colorWithAlphaComponent:0.5];
        
        [self addSubview:self.topImg];
        [self.topImg addSubview:self.topLabel];
        
        [self addSubview:self.contImg];
        [self.contImg addSubview:self.collection];
        
        UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickGradeView)];
        tapp.delegate = self;
        [self addGestureRecognizer:tapp];
    }
    return self;
}

// 点击空白隐藏
-(void)clickGradeView {
    self.hidden = YES;
    
    if (self.didGradeBackViewBlock) {
        self.didGradeBackViewBlock();
    }
}

// 点击手势响应者
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.topImg] || [touch.view isDescendantOfView:self.contImg]) {
        return NO;
    }
    return YES;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self.contImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        //make.width.mas_offset(ScreenWidth/2.86);
        make.width.mas_offset(400);
        make.height.mas_equalTo(self.contImg.mas_width).dividedBy(1.4);
    }];
    
    [self.topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contImg.mas_top);
        make.left.equalTo(self.contImg.mas_left);
        make.width.mas_equalTo(self.contImg.mas_width).dividedBy(1.4);
        make.height.mas_equalTo(self.topImg.mas_width).dividedBy(5.57);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topImg);
        make.left.equalTo(self.topImg).offset(15);
        make.right.equalTo(self.topImg).offset(-15);
        make.top.bottom.equalTo(self.topImg);
    }];
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contImg);
        make.top.equalTo(self.contImg).offset(10);
        make.bottom.equalTo(self.contImg.mas_bottom).offset(-15);
    }];
}

-(void)setArrGradeList:(NSArray *)arrGradeList {
    _arrGradeList = arrGradeList;
    [self.collection reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
